package vn.fsoft.gst.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import vn.fsoft.gst.domain.SupportingMaterial;

/**
 * This class SupportingMaterialDAOImpl implement SupportingMaterialDAO
 * interface
 * 
 * @author TienNM
 *
 */
@Repository
@SuppressWarnings({ "unchecked" })
public class SupportingMaterialDAOImpl implements SupportingMaterialDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public int addSupportingMaterial(SupportingMaterial supportingMaterial) {
        Session session = sessionFactory.getCurrentSession();
        return (Integer) session.save(supportingMaterial);
    }

    @Transactional
    public void editSupportingMaterial(SupportingMaterial supportingMaterial) {
        Session session = sessionFactory.getCurrentSession();
        session.merge(supportingMaterial);
    }

    @Transactional
    public void deleteSupportingMaterial(int Id) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(session.load(SupportingMaterial.class, Id));
    }

    @Transactional
    public SupportingMaterial getById(int Id) {
        Session session = sessionFactory.getCurrentSession();
        return (SupportingMaterial) session.get(SupportingMaterial.class, Id);
    }

    @Transactional
    public List<SupportingMaterial> getAll() {
        Session session = sessionFactory.getCurrentSession();
        List<SupportingMaterial> list = session.createQuery(
                "from SupportingMaterial").list();
        return list;
    }

    public void activeSupportingMaterial(int Id) {
        Session session = sessionFactory.getCurrentSession();
        SupportingMaterial supportingMaterial = getById(Id);
        supportingMaterial.setActive(true);
        session.merge(supportingMaterial);

    }

    @Transactional
    public List<SupportingMaterial> getByOrg(int orgID) {
        Session session = sessionFactory.getCurrentSession();
        List<SupportingMaterial> list = session.createQuery(
                "from SupportingMaterial where orgID=" + orgID).list();
        return list;
    }

}
